package hu.uniobuda.nik.bsik2f;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

public class LibraryActivity extends Activity {

    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_library);

        if (Build.VERSION.SDK_INT > 11) {
            actionBar = getActionBar();
            actionBar.setTitle(R.string.title_activity_library);
        }

        boolean state = Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState());
        final File musicDir;
        final List<TagInfo> tags = null;

        if (state) {
            musicDir = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_MUSIC);
        } else {
            musicDir = new File("/storage");
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                LoadFiles(musicDir,new Package(tags));
            }
        }).run();

        TagAdapter adapter = new TagAdapter(this, R.layout.listitem_layout,
                tags);

        ListView libraryListView = (ListView) findViewById(R.id.libraryListView);
        libraryListView.setAdapter(adapter);
        libraryListView
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View sender,
                                            int position, long id) {
                        TagHolder holder = (TagHolder) sender.getTag();
                        Toast.makeText(
                                LibraryActivity.this,
                                holder.filePath,
                                Toast.LENGTH_LONG).show();
                    }
                });
    }

    private void LoadFiles(File root,Package wrappedTags) {
        // List<TagInfo> tags = new LinkedList<TagInfo>();
        List<File> files = new ArrayList<File>();
        files = Utility.iterateFiles(files, root);

        if (files != null) {
            for (File f : files) {
                if (TagInfo.isSupported(f)) {
                    tags.add(new TagInfo(f.getAbsolutePath()));
                    // TODO
                }
                if (tags.size() == 0) {
                    AlertDialog.Builder ad = new AlertDialog.Builder(
                            LibraryActivity.this);
                    ad.setTitle(R.string.files_notfound_title);
                    ad.setMessage(R.string.files_notfound_message);
                    ad.setPositiveButton(android.R.string.ok,
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    finish();
                                }
                            });
                    ad.show();
                }
            }

        } else {
            AlertDialog.Builder ad = new AlertDialog.Builder(
                    LibraryActivity.this);
            ad.setTitle(R.string.files_notfound_title);
            ad.setMessage(R.string.files_notfound_message);
            ad.setPositiveButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    });
            ad.show();
        }
        // return tags;

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_library, menu);
        return true;
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }
}